package cn.ibizlab.assetmanagement.assetapi.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.assetmanagement.assetapi.dto.*;
import cn.ibizlab.assetmanagement.assetapi.mapping.*;
import cn.ibizlab.assetmanagement.core.workorder.domain.EAMMonitorPoint;
import cn.ibizlab.assetmanagement.core.workorder.service.IEAMMonitorPointService;
import cn.ibizlab.assetmanagement.core.workorder.filter.EAMMonitorPointSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"测点" })
@RestController("AssetApi-eammonitorpoint")
@RequestMapping("")
public class EAMMonitorPointResource {

    @Autowired
    public IEAMMonitorPointService eammonitorpointService;

    @Autowired
    @Lazy
    public EAMMonitorPointMapping eammonitorpointMapping;

    @PreAuthorize("hasPermission(this.eammonitorpointMapping.toDomain(#eammonitorpointdto),'iBizAssetManagement-EAMMonitorPoint-Create')")
    @ApiOperation(value = "新建测点", tags = {"测点" },  notes = "新建测点")
	@RequestMapping(method = RequestMethod.POST, value = "/eammonitorpoints")
    @Transactional
    public ResponseEntity<EAMMonitorPointDTO> create(@RequestBody EAMMonitorPointDTO eammonitorpointdto) {
        EAMMonitorPoint domain = eammonitorpointMapping.toDomain(eammonitorpointdto);
		eammonitorpointService.create(domain);
        EAMMonitorPointDTO dto = eammonitorpointMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eammonitorpointMapping.toDomain(#eammonitorpointdtos),'iBizAssetManagement-EAMMonitorPoint-Create')")
    @ApiOperation(value = "批量新建测点", tags = {"测点" },  notes = "批量新建测点")
	@RequestMapping(method = RequestMethod.POST, value = "/eammonitorpoints/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMMonitorPointDTO> eammonitorpointdtos) {
        eammonitorpointService.createBatch(eammonitorpointMapping.toDomain(eammonitorpointdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eammonitorpoint" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eammonitorpointService.get(#eammonitorpoint_id),'iBizAssetManagement-EAMMonitorPoint-Update')")
    @ApiOperation(value = "更新测点", tags = {"测点" },  notes = "更新测点")
	@RequestMapping(method = RequestMethod.PUT, value = "/eammonitorpoints/{eammonitorpoint_id}")
    @Transactional
    public ResponseEntity<EAMMonitorPointDTO> update(@PathVariable("eammonitorpoint_id") String eammonitorpoint_id, @RequestBody EAMMonitorPointDTO eammonitorpointdto) {
		EAMMonitorPoint domain  = eammonitorpointMapping.toDomain(eammonitorpointdto);
        domain .setEammonitorpointid(eammonitorpoint_id);
		eammonitorpointService.update(domain );
		EAMMonitorPointDTO dto = eammonitorpointMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eammonitorpointService.getEammonitorpointByEntities(this.eammonitorpointMapping.toDomain(#eammonitorpointdtos)),'iBizAssetManagement-EAMMonitorPoint-Update')")
    @ApiOperation(value = "批量更新测点", tags = {"测点" },  notes = "批量更新测点")
	@RequestMapping(method = RequestMethod.PUT, value = "/eammonitorpoints/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMMonitorPointDTO> eammonitorpointdtos) {
        eammonitorpointService.updateBatch(eammonitorpointMapping.toDomain(eammonitorpointdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eammonitorpointService.get(#eammonitorpoint_id),'iBizAssetManagement-EAMMonitorPoint-Remove')")
    @ApiOperation(value = "删除测点", tags = {"测点" },  notes = "删除测点")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eammonitorpoints/{eammonitorpoint_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eammonitorpoint_id") String eammonitorpoint_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eammonitorpointService.remove(eammonitorpoint_id));
    }

    @PreAuthorize("hasPermission(this.eammonitorpointService.getEammonitorpointByIds(#ids),'iBizAssetManagement-EAMMonitorPoint-Remove')")
    @ApiOperation(value = "批量删除测点", tags = {"测点" },  notes = "批量删除测点")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eammonitorpoints/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eammonitorpointService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eammonitorpointMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMMonitorPoint-Get')")
    @ApiOperation(value = "获取测点", tags = {"测点" },  notes = "获取测点")
	@RequestMapping(method = RequestMethod.GET, value = "/eammonitorpoints/{eammonitorpoint_id}")
    public ResponseEntity<EAMMonitorPointDTO> get(@PathVariable("eammonitorpoint_id") String eammonitorpoint_id) {
        EAMMonitorPoint domain = eammonitorpointService.get(eammonitorpoint_id);
        EAMMonitorPointDTO dto = eammonitorpointMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取测点草稿", tags = {"测点" },  notes = "获取测点草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eammonitorpoints/getdraft")
    public ResponseEntity<EAMMonitorPointDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eammonitorpointMapping.toDto(eammonitorpointService.getDraft(new EAMMonitorPoint())));
    }

    @ApiOperation(value = "检查测点", tags = {"测点" },  notes = "检查测点")
	@RequestMapping(method = RequestMethod.POST, value = "/eammonitorpoints/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMMonitorPointDTO eammonitorpointdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eammonitorpointService.checkKey(eammonitorpointMapping.toDomain(eammonitorpointdto)));
    }

    @PreAuthorize("hasPermission(this.eammonitorpointMapping.toDomain(#eammonitorpointdto),'iBizAssetManagement-EAMMonitorPoint-Save')")
    @ApiOperation(value = "保存测点", tags = {"测点" },  notes = "保存测点")
	@RequestMapping(method = RequestMethod.POST, value = "/eammonitorpoints/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMMonitorPointDTO eammonitorpointdto) {
        return ResponseEntity.status(HttpStatus.OK).body(eammonitorpointService.save(eammonitorpointMapping.toDomain(eammonitorpointdto)));
    }

    @PreAuthorize("hasPermission(this.eammonitorpointMapping.toDomain(#eammonitorpointdtos),'iBizAssetManagement-EAMMonitorPoint-Save')")
    @ApiOperation(value = "批量保存测点", tags = {"测点" },  notes = "批量保存测点")
	@RequestMapping(method = RequestMethod.POST, value = "/eammonitorpoints/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMMonitorPointDTO> eammonitorpointdtos) {
        eammonitorpointService.saveBatch(eammonitorpointMapping.toDomain(eammonitorpointdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMMonitorPoint-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMMonitorPoint-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"测点" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eammonitorpoints/fetchdefault")
	public ResponseEntity<List<EAMMonitorPointDTO>> fetchDefault(EAMMonitorPointSearchContext context) {
        Page<EAMMonitorPoint> domains = eammonitorpointService.searchDefault(context) ;
        List<EAMMonitorPointDTO> list = eammonitorpointMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMMonitorPoint-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMMonitorPoint-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"测点" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eammonitorpoints/searchdefault")
	public ResponseEntity<Page<EAMMonitorPointDTO>> searchDefault(@RequestBody EAMMonitorPointSearchContext context) {
        Page<EAMMonitorPoint> domains = eammonitorpointService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eammonitorpointMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

